﻿/*
editor_loadfile.js

The MIT License

Copyright (c) 2011 Yuya.Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

// ファイルローダ

//! ファイルローダクラス
function EspEditorLoadFile(){
	this.audio = null;
	this.data = null;

	this.selectImages = 0;

	this.imageElement = null;
	this.audioElement = null;
}

EspEditorLoadFile.ImageViewID = "imageview";
EspEditorLoadFile.AudioViewID = "audioview";
//! タグ読込チェック感覚
EspEditorLoadFile.TagCheckSpan = 100;
//! サムネイルの高さ
EspEditorLoadFile.ThumbHeightMax = 120;
EspEditorLoadFile.ThumbHeight = EspEditorLoadFile.ThumbHeightMax;

//! EspEditorBaseを継承
EspEditorLoadFile.prototype = new EspEditorBase(espMessage.window.editorLoadFile, 500, 600);
EspEditorLoadFile.prototype.constructor = EspEditorLoadFile;

EspEditorLoadFile.tagNamePreview = "preview";
EspEditorLoadFile.tagNameFileName = "filename";

//! 再描画関数をオーバーライドする
EspEditorLoadFile.prototype.redraw = function(){
	var text = "";
	text += "<table width='100%' height='100%'>";
	text += "<tr><td>";
	text += "<iframe width='100%' height='100%' id='"+EspEditorLoadFile.ImageViewID+"'></iframe>";
	text += "</td></tr>";
	text += "<tr><td>";
	text += "<iframe width='100%' height='100%' id='"+EspEditorLoadFile.AudioViewID+"'></iframe>";
	text += "</td></tr>";	
	text += "<tr><td style='width:400px;height:100px;' ondragover='window.subcomponent.dragFile(event);' ondrop='window.subcomponent.dropFile(event);'>";
	text += espMessage.editorLoadFile.dropHere + "</td></tr>";
	text += "<tr><td id='"+EspEditorLoadFile.tagNamePreview+"'>";
	text += "</td></tr>";
	text += "</table>";
	this.writedoc(text);
	this.initImageViewer();
	this.initAudioViewer();
}

EspEditorLoadFile.prototype.dragFile = function(event){
	if (event.dataTransfer.types[0] == "Files"){
		event.preventDefault();
	}
}

//! ドロップされたファイルを扱う
EspEditorLoadFile.prototype.dropFile = function(event){
	var file = event.dataTransfer.files[0];
	if(file.type.match(/image\/\w+/)){
		this.dropImage(event, file);
		return;
	}else if(file.type.match(/audio\/\w+/)){
		this.dropAudio(event, file);
		return;
	}
}

//! ドロップされた画像を扱う
EspEditorLoadFile.prototype.dropImage = function(event, file){
	var filename = event.dataTransfer.files[0].fileName;
	var reader = new FileReader();
	reader.filename = filename;
	var _this = this;
	// エンコード完了後のコールバック
	reader.onloadend = function() {
		_this.previewImage(reader);
	}
	// Base64URI変換
	reader.readAsDataURL(file);
}

//! ドロップされた音データを扱う
EspEditorLoadFile.prototype.dropAudio = function(event, file){
	var filename = event.dataTransfer.files[0].fileName;
	var reader = new FileReader();
	reader.filename = filename;
	var _this = this;
	// エンコード完了後のコールバック
	reader.onloadend = function() {
		_this.previewAudio(reader);
	}
	// Base64URI変換
	reader.readAsDataURL(file);
}

//! 画像プレビュー
EspEditorLoadFile.prototype.previewImage = function(reader){
	var td = this.window.document.getElementById(EspEditorLoadFile.tagNamePreview);
	if(reader.error){
		// エラーが発生
		td.innerHTML = "filereader error.<br>"+reader.filename+" erro code = " + reader.error.code;
		return;
	}
	this.data = reader.result;
	var text = "";
	text += "<input type='text' value='"+reader.filename+"' name='"+EspEditorLoadFile.tagNameFileName+"'><button onclick='window.subcomponent.addImage();'>"+espMessage.editorLoadFile.regist+"</button><br>";
	text += "<img src='"+reader.result+"'>";
	td.innerHTML = text;
}

//! Audioプレビュー
EspEditorLoadFile.prototype.previewAudio = function(reader){
	var td = this.window.document.getElementById(EspEditorLoadFile.tagNamePreview);
	if(reader.error){
		// エラーが発生
		td.innerHTML = "filereader error.<br>"+reader.filename+" erro code = " + reader.error.code;
		return;
	}
	this.data = reader.result;
	this.audio = new Audio(reader.result);
	var text = "";
	text += "<input type='text' value='"+reader.filename+"' name='"+EspEditorLoadFile.tagNameFileName+"'><button onclick='window.subcomponent.addAudio();'>"+espMessage.editorLoadFile.regist+"</button><br>";
	text += "<button onclick='window.subcomponent.previewAudioPlay();'><image src='img/sound_play.png'></button>";
	td.innerHTML = text;
}

EspEditorLoadFile.prototype.previewAudioPlay = function(){
	if(this.audio){
		this.audio.play();
	}
}

EspEditorLoadFile.prototype.previewAudioPlayByData = function(data){
	var audio = new Audio(data);
	audio.play();
}

//! 画像追加
EspEditorLoadFile.prototype.addImage = function(){
	// 名前
	var names = this.window.document.getElementsByName(EspEditorLoadFile.tagNameFileName);
	espResource.addImage(names[0].value, this.data);
	this.viewImage();
	// 画像の追加を終えたらプレビュー領域を空にする
	var td = this.window.document.getElementById(EspEditorLoadFile.tagNamePreview);
	if(!td){
		return;
	}
	td.innerHTML = "";
}

//! 音追加
EspEditorLoadFile.prototype.addAudio = function(){
	// 名前
	var names = this.window.document.getElementsByName(EspEditorLoadFile.tagNameFileName);
	espResource.addAudio(names[0].value, this.data);
	this.viewAudio();
	// 追加を終えたらプレビュー領域を空にする
	var td = this.window.document.getElementById(EspEditorLoadFile.tagNamePreview);
	if(!td){
		return;
	}
	td.innerHTML = "";
}


//! 画像ビューワの初期化
EspEditorLoadFile.prototype.initImageViewer = function(){
	this.imageElement = this.window.document.getElementById(EspEditorLoadFile.ImageViewID);
	if(!this.imageElement){
		var _this = this;
		setTimeout(function(){_this.initImageViewer();}, EspEditorLoadFile.TagCheckSpan);
		return;
	}
	EspEditorLoadFile.ThumbHeight = Math.floor(this.imageElement.offsetHeight * 0.6);
	if(EspEditorLoadFile.ThumbHeight > EspEditorLoadFile.ThumbHeightMax){
		EspEditorLoadFile.ThumbHeight = EspEditorLoadFile.ThumbHeightMax;
	}
	this.viewImage();
}

//! 音ビューワの初期化
EspEditorLoadFile.prototype.initAudioViewer = function(){
	this.audioElement = this.window.document.getElementById(EspEditorLoadFile.AudioViewID);
	if(!this.audioElement){
		var _this = this;
		setTimeout(function(){_this.initAudioViewer();}, EspEditorLoadFile.TagCheckSpan);
		return;
	}
	this.viewAudio();
}

//! 追加済み画像ビューワ
EspEditorLoadFile.prototype.viewImage = function(){
	// iframe要素にスタイルシートを適用する
	var cont = this.imageElement.contentDocument;
	cont.open();
	var text = "";
	text += "<html><head>";
	text += "<link rel='stylesheet' href='css/develop.css'　type='text/css'>";
	text += "</head><body class='develop'>";
	text += "<nobr>";
	// 全ての画像を描画する
	for(var i = 0; i < espResource.getImageCount(); i++){
		var data = espResource.getImage(i);
		var w = (data.width / data.height) * EspEditorLoadFile.ThumbHeight;
		var h = EspEditorLoadFile.ThumbHeight;
		var t = "";
		t += "<table style='float:left;' class='hover'>";
		t += "<tr><td>"+data.name+"</td></tr>";
		t += "<tr><td><img src='"+data.data+"' width='"+w+"' height='"+h+"'></td></tr>";
		t += "<tr><td>"+data.width+"x"+data.height+"</td></tr>";
		t += "</table>";
		text += t;
	}
	text += "</nobr>";
	text += "</body></html>";
	cont.write(text);
	cont.close();
}

//! 追加済み音ビューワ
EspEditorLoadFile.prototype.viewAudio = function(){
	// iframe要素にスタイルシートを適用する
	var cont = this.audioElement.contentDocument;
//	cont.subcomponent = this;
	cont.open();
	var text = "";
	text += "<html><head>";
	text += "<link rel='stylesheet' href='css/develop.css'　type='text/css'>";
	text += "</head><body class='develop'>";
	text += "<nobr>";
	// 全てのaudioプレビューを描画する
	for(var i = 0; i < espResource.getAudioCount(); i++){
		var data = espResource.getAudio(i);
		var t = "";
		t += "<table style='float:left;'>";
		t += "<tr><td>"+data.name+"</td></tr>";
		var es = '"';
		t += "<tr><td><button onclick='window.subcomponent.previewAudioPlayByData("+es+data.data+es+");'><image src='img/sound_play.png'></button></td></tr>";
		t += "</table>";
		text += t;
	}
	text += "</nobr>";
	text += "</body></html>";
	cont.write(text);
	cont.close();
}

var espEditorLoadFile = new EspEditorLoadFile();

